package com.wannengbang.storemobile.homepage;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.ServiceSettings;
import com.google.gson.Gson;
import com.unionpay.UPPayAssistEx;
import com.wannengbang.storemobile.R;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.BankDeposiAddressListBean;
import com.wannengbang.storemobile.bean.BaseResponseBean;
import com.wannengbang.storemobile.bean.IntelligentCashierBean;
import com.wannengbang.storemobile.bean.SubBranchListBean;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.homepage.model.IHomePageModel;
import com.wannengbang.storemobile.mine.model.MineModelImpl;
import com.wannengbang.storemobile.utils.AMapManager;
import com.wannengbang.storemobile.utils.SPManager;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.AppTitleBar;
import com.wannengbang.storemobile.widget.ViewLoading;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class IntelligentCashierActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.edit_money)
    EditText edit_money;
    @BindView(R.id.tv_commit)
    TextView tv_commit;
    @BindView(R.id.cb_zfb)
    CheckBox cb_zfb;
    @BindView(R.id.cb_ysf)
    CheckBox cb_ysf;

    private AMapManager aMapManager;
    private IHomePageModel homePageModel = new HomePageModelImpl();
    private double latitude;
    private double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intelligent_cashier);
        ButterKnife.bind(this);
        getCurrentLocationLatLng();
        initEvent();
    }

    public void getCurrentLocationLatLng(){

        aMapManager = new AMapManager();
        aMapManager.getOnceLocation(aMapLocation -> {
            aMapManager.onDestroy();
            if (aMapLocation.getErrorCode() == 0) {//获得了定位

                latitude = aMapLocation.getLatitude();
                longitude = aMapLocation.getLongitude();

                Log.e("未读",aMapLocation.getLongitude()+"");

            } else {
                ToastUtil.showShort("定位失败，检查定位权限");
            }
        });
    }
    String third_type;

    public void initEvent(){
        cb_zfb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    third_type = "2";
                    cb_ysf.setChecked(false);
                }
            }
        });
        cb_ysf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    third_type = "3";
                    cb_zfb.setChecked(false);
                }
            }
        });



        tv_commit.setOnClickListener(v -> {

            String money = edit_money.getText().toString().trim();

            if (TextUtils.isEmpty(money)) {
                ToastUtil.showShort("请输入金额");
                return;
            }

            if (!cb_zfb.isChecked()&&!cb_ysf.isChecked()){
                ToastUtil.showShort("请选择支付方式");
                return;
            }



            ViewLoading.showProgress(mActivity, "加载中......");
            double v1 = Double.valueOf(money) * 100;
            homePageModel.smartCollection(String.valueOf(v1), latitude,longitude,third_type, new DataCallBack<IntelligentCashierBean>() {
                @Override
                public void onSuccessful(IntelligentCashierBean bean) {

                    String url = bean.getData().getUrl();

                    ViewLoading.dismissProgress();

                    if ("2".equals(third_type)){
                        //支付宝
                        jumpAlipays(url);
                    }else if ("3".equals(third_type)){
                        //云闪付
                        yunShaFu(url);
                    }


                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });

        });
    }

    //跳转到支付宝付款界面
    private void jumpAlipays(String payCode) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri uri = null;
            try {
                String s = "alipays://platformapi/startapp?saId=10000007&qrcode=" + URLEncoder.encode(payCode, "UTF-8");
                Log.e("跳转链接=",s);
                uri = Uri.parse(s);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(uri);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //跳转到云闪付app
    private void yunShaFu(String payCode) {

        UPPayAssistEx.startPay(this, null, null, payCode, "00");


    }
}